package com.example.demo;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * <p>
 * 测试生成代码
 * </p>
 */
public class GeneratorServiceEntity {

    @Test
    public void generateCode() {
        String packageName = "com.sing";//
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
        String[] tableNames={"t_sing_user"};//表明
        generateByTables(serviceNameStartWithI, packageName,tableNames);
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String[] tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://localhost:3306/flower-sing";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("demo")
                .setPassword("demo")
                .setDriverName("com.mysql.jdbc.Driver");//加载jar
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
//                .setCapitalMode(true)//全局大写命名 ORACLE 注意
                .setEntityLombokModel(false)
                .setTablePrefix(new String[] { "t_sing_" })//此处可以修改为您的表前缀
                .setDbColumnUnderline(true)//在MP中，我们建议数据库表名 和 表字段名采用驼峰命名方式， 如果采用下划线命名方式 请开启全局下划线开关。
                .setNaming(NamingStrategy.underline_to_camel)//生成表的策略
                .setInclude(tableNames);//表名传数组
        config.setActiveRecord(true)//不需要ActiveRecord特性的请改为false
                .setAuthor("yinhuachang")
                .setOutputDir("d:\\mybatisGen")//本地地址
                .setFileOverride(true)
                .setEnableCache(false)// XML 二级缓存
                .setBaseResultMap(true)// XML ResultMap
                .setBaseColumnList(true);// XML columList
                // 自定义文件命名，注意 %s 会自动填充表实体属性！
                // gc.setMapperName("%sDao");
                // gc.setXmlName("%sDao");
                // gc.setServiceName("%sService");
                // gc.setServiceImplName("%sServiceDiy");
                // gc.setControllerName("%sAction");
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()//具体生成的代码
                                .setParent(packageName)
//                                .setController("controller")
                                .setEntity("entity")
                                .setMapper("mapper")
                                .setServiceImpl("serviceImpl")
                                .setService("service")
                                .setXml("mapxml")
                ).execute();
    }

}
